﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PinCity.Models.Repositories;
using PinCity.Models.Content;
using PinCity.Localization;

namespace PinCity.Controllers
{
    public class PinsController : BaseController
    {
        private PinRepository _pins = new PinRepository();
        private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(typeof(PinsController));

        public ActionResult Index()
        {
            return this.All();
        }

        /// <summary>
        /// Gets all pins. Accepts AJAX POST request (returns JSON array)
        /// and common get request (returns a View).
        /// </summary>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)]
        public ActionResult All()
        {
            ActionResult result = null;
            if (this.Request.IsAjaxRequest())
            {
                logger.Debug("All pins requseted with ajax");
                result = this.Json(this._pins.GetAll().ToArray(), JsonRequestBehavior.AllowGet);
            }
            else
            {
                logger.Debug("All pins requseted");
                result = this.View(this._pins.GetAll());
            }
            return result;
        }

        /// <summary>
        /// Displays details of one pin. Accepts
        /// AJAX POST request (returns JSON object) and
        /// common get request (returns a View).
        /// </summary>
        /// <param name="id">ID of desired pin.</param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
        public ActionResult Details(uint id)
        {
            ActionResult result = null;
            Pin pin = this._pins.Get(id);
            if (this.Request.IsAjaxRequest())
            {
                result = this.Json(pin, JsonRequestBehavior.AllowGet);
            }
            else
            {
                if (pin != null)
                {
                    result = this.View(pin);
                }
                else
                {
                    result = this.Error404(LocalizationManager.GetString("pinNotFoundMessage"));
                }
            }
            return result;
        }

        /// <summary>
        /// Displays a form for pin creation.
        /// Accepts common GET requests.
        /// </summary>
        /// <returns></returns>
        public ActionResult Create()
        {
            return this.View();
        }

        /// <summary>
        /// Accepts a pin and stores it in repo. Accepts AJAX POST
        /// request (returns true as JSON) and common POST request
        /// (returns redirect).
        /// </summary>
        /// <param name="pin">Pin to save.</param>
        /// <returns></returns>
        // [Authorize] // TODO: Uncomment this, when authentication will be fully implemented
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Pin pin)
        {
            ActionResult result = null;
            this._pins.SaveOrUpdate(pin);
            if (this.Request.IsAjaxRequest())
            {
                logger.Debug("Pin creation invoked via AJAX");
                result = this.Json(true);
            }
            else
            {
                logger.Debug("Pin creation invoked");
                result = this.RedirectToAction("Details", pin.ID);
            }
            return result;
        }

        /// <summary>
        /// Shows a confirmation window for pin deletion.
        /// </summary>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Get)]
        // [Authorize] // TODO: Uncomment this, when authentication will be fully implemented
        public ActionResult Delete()
        {
            return this.View();
        }

        /// <summary>
        /// Actually deletes a pin from repo.
        /// TODO: Implement a check, that allows to delete pin
        /// only to author or admin.
        /// </summary>
        /// <param name="id">ID of pin, that needs to be deleted.</param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        // [Authorize] // TODO: Uncomment this, when authentication will be fully implemented
        public ActionResult Delete(uint id)
        {
            ActionResult result = null;
            Pin pinToDelete = this._pins.Get(id);
            if (pinToDelete != null)
            {
                this._pins.Delete(pinToDelete);
                result = this.RedirectToAction("All");
            }
            else
            {
                result = this.Error404("pinNotFoundMessage");
            }
            return result;
        }
    }
}
